home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / NewsBase / Source / IFolderDockMatrix.m < prev    next >
Text File  |  1993-01-12  |  9KB  |  313 lines

  1. /*$Copyright:
  2.  * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
  3.  * Institute for Supercomputing Research
  4.  * All rights reserved.
  5.  * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
  6.  *
  7.  * You may freely copy, distribute and reuse the code in this program under 
  8.  * following conditions.
  9.  * - to include this notice in the source code, if it is to be distributed 
  10.  *   with source code.
  11.  * - to add the file named "COPYING" within the code, which shall include 
  12.  *   GNU GENERAL PUBLIC LICENSE(*).
  13.  * - to display an acknowledgement in binary code as follows: "This product
  14.  *   includes software developed by Recruit Co.,Ltd., ISR."
  15.  * - to display a notice which shall state that the users may freely copy,
  16.  *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
  17.  *   LICENSE(*)
  18.  * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
  19.  *
  20.  *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
  21.  * 
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  23.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  24.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25. $*/
  26.  
  27. #import "IFolderDockMatrix.h"
  28. #import "IFolderDockCell.h"
  29. #import "IFolderTransparentWindow.h"
  30. #import "Localization.h"
  31. #import "errdebug.h"
  32.  
  33. #import <appkit/Font.h>
  34. #import <appkit/NXImage.h>
  35. #import <appkit/NXSplitView.h>
  36. #import <appkit/Application.h>
  37. #import <appkit/Panel.h>
  38. #import <objc/List.h>
  39.  
  40. #import <math.h>
  41. #import <string.h>
  42.  
  43. #define LoStr(key)      doLocalString(NULL,key,NULL)
  44.  
  45. //#define        DOCKCELL_W    160
  46. #define        DOCKCELL_W    120
  47. #define        DOCKCELL_H    76
  48.  
  49. @implementation IFolderDockMatrix
  50.  
  51. - initFrame:(NXRect *)frameRect
  52. {
  53.     NXSize    dockCellSize;
  54.     id        dockProto;
  55.     id        titleFont;
  56.  
  57.     // make proto type for matrix's cell
  58.     dockProto = [[IFolderDockCell allocFromZone:[self zone]] 
  59.                             initIconCell:BLANKICON];
  60.     [dockProto setTitle:""];
  61.     [dockProto setBordered:NO];
  62.     [dockProto setIconPosition:NX_ICONABOVE];
  63.     [dockProto setTarget:self];
  64.     [dockProto setAction:@selector(selectNewsgroupInBrowser:)];
  65.     titleFont = [Font newFont:TITLEFONT size:TITLEFONTSIZE];
  66.     [dockProto setFont:titleFont];
  67.     [dockProto setLeaf:YES];
  68.  
  69.     // compute # of rows and cols to fit into frameRect
  70.     dockCellSize.width = DOCKCELL_W;
  71.     dockCellSize.height = DOCKCELL_H;
  72.     // make one cell which can hold root(nextStation) icon at least
  73.     // after resizeing this view, columns and rows are added
  74.     [super initFrame:frameRect mode:NX_TRACKMODE prototype:dockProto
  75.                             numRows:1 numCols:1];
  76.     
  77.     [self setCellSize:&dockCellSize];
  78.     [self setBackgroundGray:NX_LTGRAY];
  79.     [self setCellBackgroundGray:NX_LTGRAY];
  80.     [self setEnabled:YES];
  81.     
  82.     isPalette = NO;
  83.     // set transparentWindow class
  84.     iTransparentWindowClass = [IFolderTransparentWindow class];
  85.     
  86.     // set file name which have information of dock
  87.     strncpy (iDirFileName, NXHomeDirectory(), sizeof(iDirFileName)-1);
  88.     if (strlen(iDirFileName) > (MAXPATHLEN-DIRFILELEN-1)) {
  89.     // path name of home directory is too long
  90.     fprintf(stderr,"NewsBase:IFolderDockMatrix: can't set dirfile name\n");
  91.     iDirFileName[0] = '\0';
  92.     } else {
  93.     strncat (iDirFileName, DIRFILE, DIRFILELEN);
  94.     }
  95.     
  96.     [self readDirFile];
  97.     [self sizeToCells];
  98.     
  99.     return self;
  100. }
  101.  
  102. - sizeTo:(NXCoord)width :(NXCoord)height
  103. {
  104.     int        ncols, nrows;
  105.     int        i;
  106.     NXSize    frameSize;
  107.     
  108.     DBG(10,fprintf(stderr," --size.width=%f\t size.height=%f\n",
  109.                     frame.size.width,frame.size.height));
  110.     DBG(10,fprintf(stderr," --numRows=%d\t numCols=%d\n", numRows, numCols));
  111.  
  112.     frameSize.width = width; frameSize.height = height;
  113.     [self getEnoughNumRows:&nrows numCols:&ncols forSize:&frameSize];
  114.     
  115.     // if columns or rows are short for this size, make new cells
  116.     if (numCols < ncols) {
  117.     // make columns
  118.     for (i = ncols - numCols; i > 0; --i) {
  119.         [self addCol];
  120.     }
  121.     }    
  122.     if (numRows < nrows) {
  123.     // make rows
  124.     for (i = nrows - numRows; i > 0; --i) {
  125.         [self addRow];
  126.     }
  127.     }
  128.     
  129.     DBG(10,fprintf(stderr," ++size.width=%f\t size.height=%f\n",
  130.                     frame.size.width,frame.size.height));
  131.     DBG(10,fprintf(stderr," ++numRows=%d\t numCols=%d\n", numRows, numCols))
  132.     return ([super sizeTo:width :height]);
  133. }
  134.  
  135. - saveDirFile
  136. {
  137.     NXStream    *dirMemStream;
  138.     int        i;
  139.     int        irow, icol;
  140.     id        tcell;
  141.     char    buf[256];
  142.     
  143.     if (strlen(iDirFileName) == 0) {
  144.     // dir file name is not set yet
  145.     return NULL;
  146.     }
  147.     if ((dirMemStream = NXOpenMemory(NULL, 0, NX_WRITEONLY)) == NULL) {
  148.     // could not open stream
  149.     fprintf(stderr," NewsBase:IFolderDockMarix: memory open error\n");
  150.     return NULL;
  151.     }
  152.     
  153.     // element at 0 is root(nextStatin), so does not need to save
  154.     i = 1;
  155.     while (tcell=[cellList objectAt:i]) {
  156.     // cellList holds all cell in this matrix
  157.     if ([tcell title] == NULL) {
  158.         // pointer of title should not be NULL
  159.         [tcell setTitle:""];
  160.     }
  161.     if (strlen([tcell title]) != 0) {
  162.         // cell is not blank cell
  163.         [self getRow:&irow andCol:&icol ofCell:tcell];
  164.         NXPrintf(dirMemStream,"%d %d %s\n", irow, icol, [tcell title]);
  165.     }
  166.     ++i;
  167.     }
  168.     if (NXSaveToFile (dirMemStream, iDirFileName) != 0 ) {
  169.     sprintf(buf,"error occured writing data to file \"%.90s\"",
  170.                             iDirFileName);
  171.     NXRunAlertPanel(LoStr("NewsBase"),LoStr(buf),LoStr("OK"),NULL,NULL);
  172.     }
  173.     NXCloseMemory(dirMemStream, NX_FREEBUFFER);
  174.     return self;
  175. }
  176.  
  177. - readDirFile
  178. {
  179.     NXStream    *fileStream;
  180.     int        irow, icol;
  181.     char    cellTitle[128];
  182.     int        i;
  183.     id        tcell;
  184.     
  185.     if (strlen(iDirFileName) == 0) {
  186.     return NULL;
  187.     }
  188.     if ((fileStream = NXMapFile(iDirFileName, NX_READONLY)) == NULL) {
  189.     // file mapping failed
  190.     fprintf(stderr,"INewsBase:IFolderDockMatrix: can't map .dir.newsbase"
  191.             " file\n");
  192.     return NULL;
  193.     }
  194.     while (NXScanf(fileStream, "%d %d %127s\n",&irow,&icol,cellTitle) == 3) {
  195.     if (irow > (numRows-1)) {
  196.         // not enough rows
  197.         for (i = numRows; i <= irow; ++i) {
  198.         [self addRow];
  199.         }
  200.     }
  201.     if (icol > (numCols-1)) {
  202.         // not enough cols
  203.         for (i = numCols; i <= icol; ++i) {
  204.         [self addCol];
  205.         }
  206.     }
  207.     // set cell stored in .dir.newsbase
  208.     tcell = [self cellAt:irow :icol];
  209.     [tcell setImage:[NXImage findImageNamed:CLOSEDFOLDER]];
  210.     [tcell setTitle:cellTitle];
  211.     }
  212.     NXCloseMemory (fileStream, NX_FREEBUFFER);
  213.     return self;
  214. }
  215.  
  216. - (void)getEnoughNumRows:(int *)rows numCols:(int *)cols 
  217.                 forSize:(NXSize *)frameSize
  218. {
  219.     *cols = floor (frameSize->width/(cellSize.width + intercell.width));
  220.     *rows = rint (frameSize->height/(cellSize.height + intercell.height));
  221.     return;
  222. }
  223.  
  224.  
  225. ///* delegate method for NXSplitView */
  226. //
  227. //#define        SCRLLMATRIX_HEIGHT    107  // height for oNewsfolderScroll
  228. //
  229. //- splitView:sender resizeSubviews:(const NXSize *)oldSize
  230. //{
  231. //    id        upperView, lowerView;
  232. //    NXRect    viewFrame, upperFrame;
  233. //    int        ncols, nrows;
  234. //    int        i, j;
  235. //    
  236. //    DBG(1,fprintf(stderr,"-- splitView:\n"));
  237. //    upperView = *(NX_ADDRESS([sender subviews]));
  238. //    lowerView = *(NX_ADDRESS([sender subviews]) + 1);
  239. //
  240. //    [[sender window] disableDisplay];
  241. //    [sender adjustSubviews];
  242. //
  243. //    // adjust upper(self) view size
  244. //    [sender getFrame:&viewFrame];
  245. //    [upperView getFrame:&upperFrame];    // IFolderDockMatrix rectangle
  246. //    [self getEnoughNumRows:&nrows numCols:&ncols 
  247. //                        forSize:&(upperFrame.size)];
  248. //    upperFrame.size.height = cellSize.height * nrows +
  249. //                             intercell.height * (nrows-1);
  250. //    [upperView setFrame:&upperFrame];
  251. //    
  252. //    // compute lower view size
  253. //    viewFrame.origin.y = [sender dividerHeight] + upperFrame.size.height;
  254. //                        // coord. is flipped in NXSplitView
  255. //    viewFrame.size.height -= viewFrame.origin.y;
  256. //    [lowerView setFrame:&viewFrame];    
  257. //
  258. //    [[sender window] reenableDisplay];
  259. //    
  260. //    // unregister cell's frame to viewRectList in AcceptWindow
  261. //    // so that the clipped cell can not be used as dropped well
  262. //    
  263. //    if (numCols > ncols) {
  264. //    // remove columns
  265. //    for (i = (numCols-1); i > (ncols-1); --i) {
  266. //        // unregister rect of cell which will be clipped by ScrollView,
  267. //        // which is a superview of this class
  268. //        for (j = (numRows-1); j >= 0; --j) {
  269. //        [self unregisterCellAt:j :i window:window];
  270. //        }
  271. //    }
  272. //    }
  273. //    
  274. //    if (numRows > nrows) {
  275. //    // remove rows
  276. //    for (i = (numRows-1); i > (nrows-1); --i) {
  277. //        // unregister rect of cell which will be clipped by ScrollView,
  278. //        // which is a superview of this class
  279. //        for (j = (numCols-1); j >= 0; --j) {
  280. //        [self unregisterCellAt:i :j window:window];
  281. //        }
  282. //    }
  283. //    }
  284. //    
  285. //    return self;
  286. //}
  287. //
  288. //- splitView:sender getMinY:(NXCoord *)minY maxY:(NXCoord *)maxY
  289. //                        ofSubviewAt:(int)offset
  290. //{
  291. //    NXRect    viewFrame;
  292. //    int        nrows, ncols;
  293. //
  294. //    *minY = 0.0;
  295. //    [sender getFrame:&viewFrame];
  296. //    viewFrame.size.height -= SCRLLMATRIX_HEIGHT;
  297. //    [self getEnoughNumRows:&nrows numCols:&ncols 
  298. //                        forSize:&(viewFrame.size)];
  299. //    nrows -= 1;            // avoid that the upperview size will be 
  300. //                    // overflow because of rounding nrows
  301. //    if (nrows >= 1) {
  302. //    *maxY = cellSize.height * nrows + intercell.height * (nrows -1);
  303. //    } else {
  304. //    *maxY = 0.0;
  305. //    }
  306. //    
  307. //    return self;
  308. //}
  309. //
  310.  
  311.  
  312. @end
  313.